package per.jdz.single;

/**
 * 懒汉
 */
public class LazyMan {
   
   private LazyMan() {
        System.out.println(Thread.currentThread().getName());
   };
   
//    private static volatile LazyMan instance;
   private static LazyMan instance;
   
   private static LazyMan getInstance() {
        // 一重：防止不必要的开销
        if (instance == null) {
            synchronized(LazyMan.class) {
                // 两重：防止有线程已经创建了实例而继续创建
                if (instance == null) {
                    instance = new LazyMan();
                }
            }
        }
        return instance;
   }
//    private static LazyMan getInstance() {
//         if (instance == null) {
//             instance = new LazyMan();
//         }
//         return instance;
//    }

   public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            new Thread(()-> {
                LazyMan.getInstance();
            }).start();
        }
   }
}
